//
//  JGDealLocalViewController.m
//  JGMeiTuanHD
//
//  Created by JJetGu on 15/10/11.
//  Copyright (c) 2015年 Free. All rights reserved.
//

#import "JGDealLocalViewController.h"
#import "UIBarButtonItem+Extension.h"
#import "JGDeal.h"
#import "JGDealCell.h"
#import "JGConst.h"
#import "MJRefresh.h"
#import "JGDealDBTool.h"

NSString *const JGDone = @"完成";
NSString *const JGEdit = @"编辑";

#define JGString(str) [NSString stringWithFormat:@"  %@  ", str]

@interface JGDealLocalViewController () <JGDealCellDelegate>

@property (nonatomic, strong) UIBarButtonItem *backItem;
@property (nonatomic, strong) UIBarButtonItem *selectAllItem;
@property (nonatomic, strong) UIBarButtonItem *unselectAllItem;
@property (nonatomic, strong) UIBarButtonItem *removeItem;

@end

@implementation JGDealLocalViewController

#pragma mark - 初始化导航栏上的所有item
- (UIBarButtonItem *)backItem
{
    if (!_backItem) {
        self.backItem = [UIBarButtonItem itemWithTarget:self action:@selector(back) image:@"icon_back" highImage:@"icon_back_highlighted"];
    }
    return _backItem;
}

- (UIBarButtonItem *)selectAllItem
{
    if (!_selectAllItem) {
        self.selectAllItem = [[UIBarButtonItem alloc] initWithTitle:JGString(@"全选") style:UIBarButtonItemStyleDone target:self action:@selector(selectAll)];
    }
    return _selectAllItem;
}

- (UIBarButtonItem *)unselectAllItem
{
    if (!_unselectAllItem) {
        self.unselectAllItem = [[UIBarButtonItem alloc] initWithTitle:JGString(@"全不选") style:UIBarButtonItemStyleDone target:self action:@selector(unselectAll)];
    }
    return _unselectAllItem;
}

- (UIBarButtonItem *)removeItem
{
    if (!_removeItem) {
        self.removeItem = [[UIBarButtonItem alloc] initWithTitle:JGString(@"删除") style:UIBarButtonItemStyleDone target:self action:@selector(remove)];
        self.removeItem.enabled = NO;
    }
    return _removeItem;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 左边的返回
    self.navigationItem.leftBarButtonItems = @[self.backItem];
    
    // 设置导航栏内容
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:JGEdit style:UIBarButtonItemStyleDone target:self action:@selector(edit)];
    
}

/**
 *  返回
 */
- (void)back {
    [self dismissViewControllerAnimated:YES completion:nil];
}

/**
 *  编辑
 */
- (void)edit
{
    NSString *title = self.navigationItem.rightBarButtonItem.title;
    if ([title isEqualToString:JGEdit]) {
        self.navigationItem.rightBarButtonItem.title = JGDone;
        
        // 进入编辑状态
        for (JGDeal *deal in self.deals) {
            deal.editing = YES;
        }
        [self.collectionView reloadData];
        
        // 左边显示4个item
        self.navigationItem.leftBarButtonItems = @[self.backItem, self.selectAllItem, self.unselectAllItem, self.removeItem];
    } else {
        self.navigationItem.rightBarButtonItem.title = JGEdit;
        
        // 结束编辑状态
        for (JGDeal *deal in self.deals) {
            deal.editing = NO;
            deal.checking = NO;
        }
        [self.collectionView reloadData];
        
        // 控制删除item的状态和文字
        [self dealCellCheckingStateDidChange:nil];
        
        // 左边显示1个item
        self.navigationItem.leftBarButtonItems = @[self.backItem];
    }
}

/**
 *  全选
 */
- (void)selectAll
{
    for (JGDeal *deal in self.deals) {
        deal.checking = YES;
    }
    [self.collectionView reloadData];
    
    // 控制删除item的状态和文字
    [self dealCellCheckingStateDidChange:nil];
}

/**
 *  删除
 */
- (void)remove
{
    
}

/**
 *  全不选
 */
- (void)unselectAll
{
    for (JGDeal *deal in self.deals) {
        deal.checking = NO;
    }
    [self.collectionView reloadData];
    
    // 控制删除item的状态和文字
    [self dealCellCheckingStateDidChange:nil];
}

#pragma mark - JGDealCellDelegate
-(void)dealCellCheckingStateDidChange:(JGDealCell *)cell
{
    BOOL deleteEnable = NO;
    int checkingCount = 0;
    // 1.删除item的状态
    for (JGDeal *deal in self.deals) {
        if (deal.isChecking) {
            deleteEnable = YES;
            checkingCount++;
        }
    }
    
    self.removeItem.enabled = deleteEnable;
    
    // 2.删除item的文字
    if (checkingCount) {
        self.removeItem.title = [NSString stringWithFormat:@"   删除(%d)   ", checkingCount];
    } else {
        self.removeItem.title = @"   删除   ";
    }
}

/**
 *  返回即将删除的团购
 */
- (NSArray *)willDeletedDeals
{
    NSMutableArray *checkingDeals = [NSMutableArray array];
    // 取出被打钩的团购
    for (JGDeal *deal in self.deals) {
        if (deal.isChecking) {
            [checkingDeals addObject:deal];
            deal.checking = NO;
            deal.editing = NO;
        }
    }
    [self.deals removeObjectsInArray:checkingDeals];
    
    [self.collectionView reloadData];
    
    // 控制删除item的状态和文字
    [self dealCellCheckingStateDidChange:nil];
    
    return checkingDeals;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
